/*
 *所有关于PayInfo类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PayInfoService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VoL.Domain.IRepositories;
using VoL.Domain.IServices;
using Microsoft.AspNetCore.Http.HttpResults;
using VoL.Domain.Infrastructure;
using VoL.Domain.Dtos.Patient;

namespace VoL.Domain.Services
{
    public partial class PayInfoService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBaseRepository<PayInfo> _payInfoRepo;
        private readonly IBaseRepository<Patient> _patientRepo;
        private readonly IBaseRepository<Office> _officeRepo;
        private readonly IPayInfoRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public PayInfoService(IPayInfoRepository dbRepository,IHttpContextAccessor httpContextAccessor,IBaseRepository<PayInfo> payInfoRepo,
         IBaseRepository<Patient>patientRepo, IBaseRepository<Office> officeRepo
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _payInfoRepo = payInfoRepo;
            _patientRepo = patientRepo;
            _officeRepo = officeRepo;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public Task<WebResponseContent> GetPayInfo(string partientCode)
        {
            var result =new WebResponseContent();

            var payinfolist = _payInfoRepo.GetAll();

            var patientlist = _patientRepo.GetAll();

            var patientPayInfoDTOlist = from a in payinfolist
                       join b in patientlist on a.patientCode equals b.PatientCode
                       where a.patientCode == partientCode
                       select new PatientPayInfoDTO()
                       {
                           BedCode = a.BedCode,
                           Id = a.Id,
                           IsPay = a.IsPay,
                           patientCode = a.patientCode,
                           Name=b.Name,
                           PayPlat = a.PayPlat,
                           PayTime = a.PayTime,
                           RoomCode = a.RoomCode,
                           total = a.total,
                       };

            result.OK("成功查询缴费明细", patientPayInfoDTOlist.ToList());


            return Task.FromResult(result); 
        }

        public Task<WebResponseContent> FindPayInfo(int id)
        {

            var res = new WebResponseContent();

            var patientlist = _patientRepo.GetAll();
                
            var officelist = _officeRepo.GetAll();

            var payinfolist = _payInfoRepo.GetAll(c=>c.Id== id);

            var findPatientPayInfoDTOlist =
                       from b in payinfolist
                       join c in patientlist on b.patientCode equals c.PatientCode
                       join d in officelist on c.OfficeCode equals d.OfficeCode
                       select new FindPatientPayInfoDTO()
                       {
                           OfficeName = d.OfficeName,
                           RoomCode = b.RoomCode,
                           Name = c.Name,
                           PatientCode = b.patientCode,
                           StartTime = b.PayTime,
                           total=b.total
                       };

            res.OK("查询要交费的表单成功", findPatientPayInfoDTOlist.ToList());

            return Task.FromResult(res);

        }
    }
}
